{extend name="public/base"}

{block name="title"}就行天下-设置{/block}

{block name="style"}
<link rel="stylesheet" href="__STATIC__/home/css/set.css" />
{/block}

{block name="main"}
<div class="pageControl bg-f5">
    <div class="main-top"></div>
    <div class="msui_nav msui_navTop">
        <a class="msui_navTop-back" href="javascript:history.back();"><i class="micon-back"></i></a>
        <h1 class="msui_navTop-title">收货地址管理</h1>
        <a href="" class="msui_navTop-right msui_navTop-cnav"><i class="micon-cnav"></i></a>
    </div>
    <div>
        {empty name="data"}
            <p style="text-align: center; margin-top: 4rem;">暂无收货地址</p>
        {else /}
            {volist name="data" id="vo"}
                <div class="address-list">
                    <p class="address-list-title" {neq name="Think.session.back_order_url" value=""}onclick="back_order('{$vo.id}');"{/neq}>
                        <span>{$vo.linkman}</span>
                        <span>{$vo.mobile}</span>
                    </p>
                    <p class="address-list-detail fs065" {neq name="Think.session.back_order_url" value=""}onclick="back_order('{$vo.id}');"{/neq}>{$vo.address}</p>
                    <div class="weui-cell">
                        <div class="weui-cell__hd addressMo">
                            <input type="checkbox" class="weui-check" name="chooseBox" value="address">
                            <i class="micon-check-c40 {eq name='vo.is_default' value='1'}micon-checked-c40{/eq}"></i>
                        </div>
                        <div class="weui-cell__bd addressMo-title">
                            <span>设为默认地址</span>
                        </div>
                        <div class="weui-cell__ft">
                            <a href="{:url('save_address', ['id' => $vo['id']])}" class="fs065">编辑</a>
                            <a href="javascript:;" class="fs065 remove" data-id="{$vo.id}">删除</a>
                        </div>
                    </div>
                </div>
            {/volist}
        {/empty}
    </div>
    <div class="p05 addAdressBtn">
        <a href="{:url('save_address')}" class="cl-f bg-c40 weui-btn fs065">新增收货地址</a>
    </div>
</div>
{/block}

{block name="javascript"}
<script>
    $(function(){
        $('.address-list').on('click','.addressMo',function () {
            $('.micon-check-c40').removeClass('micon-checked-c40');
            $(this).find('.micon-check-c40').addClass('micon-checked-c40');
        });
        $('.address-list').on('click','.addressMo-title',function () {
            $('.micon-check-c40').removeClass('micon-checked-c40');
            $(this).prev().find('.micon-check-c40').addClass('micon-checked-c40');
        });
        $('.address-list').on('click','.remove',function (e) {
            var _this = $(this);
            e.preventDefault();
            $.confirm('确定删除收货地址？', function () {
                $.ajax({
                    type : 'POST',
                    url : '{:url("remove_address")}',
                    data : {
                        id : _this.data('id')
                    },
                    dataType : 'json', 
                    success : function (msg) {
                        if (msg.code == 200) {
                            $.toptip('删除成功', 'success');
                            _this.parent().parent().parent().remove();
                        } else {
                            $.toptip(msg.msg);
                        }
                    }
                });
            });
            //
        });
    });

    /**
     * 点击选择一个收回地址，并且返回订单页面
     * @param id
     */
    function back_order (id) {
        var url = '{$Think.session.back_order_url}';
        var param = []; //定义一数组
        var _new_url = '';
        if (url.indexOf('address_id') > -1) {
            param = url.split("&"); //字符分割
            $.each(param, function (i, d) {
                if(d.indexOf('address_id') > -1) {
                    _new_url += 'address_id=' + id + '&';
                } else {
                    _new_url += d + '&';
                }
            });
            _new_url = _new_url.substring(0, _new_url.length-1);
        } else {
            _new_url = url + '&address_id=' + id;
        }
        window.location.href = _new_url;
    }
</script>
{/block}